<template>
  <div>
    <!-- 面包屑 -->
    <bread></bread>
    <!-- card内容区域 -->
    <el-card class="box-card">
      <el-row :span="24">
        <el-col :span="10">
          <el-input placeholder="请输入内容" v-model="input" class="input-with-select">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="14">
          <el-button type="primary" class="btn" @click="goAdd">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column type="index" label="#" width="60"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="780">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)"> </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量"> </el-table-column>
        <el-table-column prop="add_time" label="创建时间">
          <template v-slot="scope">
            {{ scope.row.add_time | time }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template>
            <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="remove(scope.row.goods_id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import bread from "../bread";
import goodsAPI from '../../api/good'
export default {
  components: { bread },
  name: "",
  data() {
    return {
      input: "",
      tableData: [],
      total: 0,
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
    };
  },
  mounted() {},
  methods: {
    //获取页面基本数据
    async goodsList() {
      const res = await goodsAPI.goodsList({
        params: this.queryInfo,
      });
      console.log(res);

      if (res.meta.status !== 200) {
        return this.$message.error("获取商品列表失败");
      }

      this.$message.success("获取商品列表成功");
      console.log(res.data);
      this.tableData = res.data.goods;
      this.total = res.data.total;
    },

    //分页
    handleSizeChange(val) {
      this.queryInfo.pagesize = val;
      // console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val;
      // console.log(`当前页: ${val}`);
    },

    //删除
    async remove(id) {
      const confirmResult = await this.$confirm(
        '此操作将永久删除该商品, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已经取消删除！')
      }
      const { data: res } = await goodsAPI.remove(id)
      if (res.meta.status !== 200) {
        return this.$message.error('删除失败！')
      }
      this.$message.success('删除成功！')
      this.goodsList()
    },
    //添加添加页面
    goAdd(){
      this.$router.push('add')
    }
  },
  computed: {},
  watch: {},
};
</script>

<style lang="scss" scoped>
.btn {
  margin-left: 10px;
}
</style>
